package com.zhlb.zfba.modules.per.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhlb.zfba.common.utils.DateUtil;
import com.zhlb.zfba.common.utils.IdGen;
import com.zhlb.zfba.modules.card.bean.CardRegInfo;
import com.zhlb.zfba.modules.card.service.CardRegInfoService;
import com.zhlb.zfba.modules.per.bean.GoodsInfo;
import com.zhlb.zfba.modules.per.bean.PoliceInfo;
import com.zhlb.zfba.modules.per.bean.RoomInfo;
import com.zhlb.zfba.modules.per.bean.SuspectInfo;
import com.zhlb.zfba.modules.per.dao.GoodsInfoDao;
import com.zhlb.zfba.modules.per.dao.PoliceInfoDao;
import com.zhlb.zfba.modules.per.dao.SuspectInfoDao;

@Service
@Transactional(readOnly = true)
public class SuspectInfoService {
	@Autowired 
	SuspectInfoDao suspectInfoDao;
	@Autowired
	CardRegInfoService cardRegInfoService;
	@Autowired
	GoodsInfoDao goodsInfoDao;
	@Autowired
	GoodsInfoService goodsInfoService;
	@Autowired
	RoomInfoService roomInfoService;
	public SuspectInfo get(String id) {
		//获取允许区域的所有房间，根据roomId，构建详情页面的允许区域：房间文字显示
		List<RoomInfo> permitRoomInfoList=null;
		SuspectInfo suspectInfo=null;
		suspectInfo=suspectInfoDao.get(id);
		if(suspectInfo!=null){
			permitRoomInfoList=roomInfoService.findList(new RoomInfo());//获取所有房间
			String banAreaIds=suspectInfo.getBanArea();
			String[] banAreaIdArr=null;
			String roomNames="";
			if(banAreaIds!=null&&permitRoomInfoList!=null){
				banAreaIdArr=banAreaIds.split(",");
				for(String tempId:banAreaIdArr){
					for(RoomInfo empRoomInfo:permitRoomInfoList){
						if(tempId.equals(empRoomInfo.getRoomID())){
							roomNames=roomNames+empRoomInfo.getRoomName()+",";
						}
					}
				}
				if(roomNames.lastIndexOf(",")!=-1){
				roomNames=roomNames.substring(0,roomNames.length()-1);
				}
			}
			suspectInfo.setBanAreaNames(roomNames);
		}
		if(suspectInfo==null){
			suspectInfo= new  SuspectInfo();
		}
		if(suspectInfo.getRoomInfo()==null){
			suspectInfo.setRoomInfo(new RoomInfo());
		}
		if(suspectInfo.getGoodsInfo()==null){
			suspectInfo.setGoodsInfo(new GoodsInfo());
		}
		return suspectInfo;
	}
	
	public List<SuspectInfo> findList(SuspectInfo suspectInfo) {
		return suspectInfoDao.findList(suspectInfo);
	}
	//获取所有同案标识，并以登记时间
	public List<SuspectInfo> findAccompliceSigns(SuspectInfo suspectInfo) {
		return suspectInfoDao.findAccompliceSigns(suspectInfo);
	}
	
//	public Page<PoliceInfo> findPage( PoliceInfo taskJobConf) {
//		return super.findPage(page, taskJobConf);
//	}
	
	@Transactional(readOnly = false)
	public void save(SuspectInfo suspectInfo) {
		CardRegInfo cardRegInfo=null;
		if(StringUtils.isNoneBlank(suspectInfo.getCardNo1())){
			CardRegInfo cardRegInfoQuery = new CardRegInfo();
			cardRegInfoQuery.setCardNo1(suspectInfo.getCardNo1());
			 cardRegInfo=cardRegInfoService.getByCardNo(cardRegInfoQuery);
		}
		if(cardRegInfo!=null){
			suspectInfo.setCardID(cardRegInfo.getCardID());
			suspectInfo.setCardNo2(cardRegInfo.getCardNo2());
		}
		//被询问人信息保存
		if(StringUtils.isBlank(suspectInfo.getInfoGUID())){
			suspectInfo.setInfoGUID(IdGen.uuid());
			suspectInfo.setInTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
			suspectInfo.setIsLeave("0");
			suspectInfoDao.insert(suspectInfo);
			GoodsInfo goodsInfo =suspectInfo.getGoodsInfo();
			goodsInfo.setGoodsGUID(IdGen.uuid());
			goodsInfo.setSuspectInfoGUID(suspectInfo.getInfoGUID());
			goodsInfo.setSuspectName(suspectInfo.getSuspectName());
			goodsInfoDao.insert(goodsInfo);
			
		}else{
			SuspectInfo updateEntity=this.get(suspectInfo.getInfoGUID());
			
			//private String suspectName;//姓名  
			updateEntity.setSuspectName(suspectInfo.getSuspectName());
			//private String suspectSex ; //性别
			updateEntity.setSuspectSex(suspectInfo.getSuspectSex());
			//#{suspectNation},  民族
			updateEntity.setSuspectNation(suspectInfo.getSuspectNation());
			//private String suspectBirth;// 出生年月 
			updateEntity.setSuspectBirth(suspectInfo.getSuspectBirth());
			//private String suspectIDNo;//  身份证号
			updateEntity.setSuspectIDNo(suspectInfo.getSuspectIDNo());
			//private String suspectAddress;//  住址
			updateEntity.setSuspectAddress(suspectInfo.getSuspectAddress());
			//private String suspectDegree;// 文化程度 
			updateEntity.setSuspectDegree(suspectInfo.getSuspectDegree());
			//private String suspectNationality; // 国籍
			updateEntity.setSuspectNationality(suspectInfo.getSuspectNationality());
			//private String suspectPlace; // 户籍地行政区划
			//updateEntity.setSuspectPlace(suspectInfo.getSuspectPlace());
			//private String suspectTel; //联系方式
			updateEntity.setSuspectTel(suspectInfo.getSuspectTel());
			//private String suspectArea; //户籍地
			updateEntity.setSuspectArea(suspectInfo.getSuspectArea());
			//carid
			updateEntity.setCardID(suspectInfo.getCardID());
			//cardi01
			updateEntity.setCardNo1(suspectInfo.getCardNo1());
			//card02
			updateEntity.setBringPolice(suspectInfo.getBringPolice());//携带民警
			updateEntity.setCardNo2(suspectInfo.getCardNo2());
			//private String suspectStature;// 身高
			updateEntity.setSuspectStature(suspectInfo.getSuspectStature());
			//private String facelFeature;// 脸型
			updateEntity.setFacelFeature(suspectInfo.getFacelFeature());
			//private String shapelFeature; //体型
			updateEntity.setShapelFeature(suspectInfo.getShapelFeature());
			//private String bloodType; //血型
			updateEntity.setBloodType(suspectInfo.getBloodType());
			//private String specialFeature;//特殊特征 
			updateEntity.setSpecialFeature(suspectInfo.getSpecialFeature());
			//private String supspocious;// 可疑依据
			updateEntity.setSupspocious(suspectInfo.getSupspocious());
			//private String suspectRemark; //备注
			updateEntity.setSuspectRemark(suspectInfo.getSuspectRemark());
			//private Integer authType ;//事由 采集信息 询问  采集信息+询问
			updateEntity.setAuthType(suspectInfo.getAuthType());
			//private String inquestRoomNo ;//询问室
			updateEntity.setInquestRoomNo(suspectInfo.getInquestRoomNo());
			//private String banArea; //banArea
			updateEntity.setBanArea(suspectInfo.getBanArea());
			//填表人
			updateEntity.setFillInName(suspectInfo.getFillInName());
			suspectInfoDao.update(updateEntity);
			//添加物品
			GoodsInfo goodsInfoUpdateEntity = goodsInfoDao.getBySuspectID(suspectInfo.getInfoGUID());
			goodsInfoUpdateEntity.setBagNo(suspectInfo.getGoodsInfo().getBagNo());//自封袋号
			goodsInfoUpdateEntity.setCupboardNo(suspectInfo.getGoodsInfo().getCupboardNo());//储物柜号
			goodsInfoUpdateEntity.setRecvPerson(suspectInfo.getGoodsInfo().getRecvPerson());//接收人
			goodsInfoUpdateEntity.setRecvTime(suspectInfo.getGoodsInfo().getRecvTime());//接收时间
			goodsInfoUpdateEntity.setGoodsInfo(suspectInfo.getGoodsInfo().getGoodsInfo());//物品内容
			//
			goodsInfoDao.update(goodsInfoUpdateEntity);
		 }
		
	}
	
	@Transactional(readOnly = false)
	public void delete(String ids) {
		String[] idArr=null;
		if(StringUtils.isNotBlank(ids)){
			idArr=ids.split(",");
			for(String id:idArr){
				suspectInfoDao.delete(id);
				//删除物品
				goodsInfoDao.deleteBySuspectInfoGUID(id);
			}
		}
		
	}
	@Transactional(readOnly = false)
	public void accompliceSign(String ids) {
		String[] idArr=null;
		SuspectInfo suspectInfo;
		if(StringUtils.isNotBlank(ids)){
			idArr=ids.split(",");
			String accompliceSign=IdGen.uuid();
			for(String id:idArr){
				suspectInfo=new SuspectInfo();
				suspectInfo.setInfoGUID(id);
				suspectInfo.setAccompliceSign(accompliceSign);
				suspectInfoDao.updateAccompliceSign(suspectInfo);
			}
		}
		
	}
	@Transactional(readOnly = false)
	public void updateIsLeave(SuspectInfo suspectInfo){
		suspectInfo.setIsLeave("1");
		suspectInfo.setLeaveTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
		suspectInfoDao.updateIsLeaveOfSuspectInfo(suspectInfo);
	}
}
